<?php
/******************************************************************************
 Report Menu
 created by Sivakumaran
 File Description:
 		To Show report menu bar in report page. this page will show all customizations for this report and links to report constraint page for change condition and to applied condition(this will show currently applied conditions.This is include file so we start coding directly.
	 	
/*****************************************************************************/



	$db->query="select
				src.customization_id,
				src.customization_name

				from
				$sys_report_customization_table src 

				where
				src.report_id=$report_id ";
	$db->runQuery();
	$custom_array=$db->returnArrays();

// 	$db->query="
// 			SELECT
// 			suf.customization_privilege
// 
// 			FROM
// 			$sys_user_filter_table suf 
// 			
// 			where 
// 			suf.report_id=$report_id and suf.user_id=$user_id and suf.customization_id=$customization_id 
// 			;";
// 
// 	$db->runQuery();
	//$customization_details_array=$db->returnArrays();
	
	$db->query="select customization_name from $sys_report_customization_table where customization_id=$customization_id and customization_name ilike 'default'";
	$db->runQuery();
	$custom_default_array=$db->returnArrays();
	
	
	$_SESSION["report"]["title"]=$report_details_array[0]["report_name"];
	
	echo "<form name=\"custom_selection\" method=\"post\" action=\"$PHP_SELF\">";
	$_POST["customizations"]=$customization_id;
	
	
	
	echo "<table width=\"100%\" border=\"0\" height=\"10px\" ><tr><td align=center>";
	
	echo "&nbsp;&nbsp;Report Formats        : ".$form->generateSelectMenu("customizations",$_POST["customizations"],$custom_array,"","",true,"onchange=doSubmit(custom_selection.customizations.options[customizations.selectedIndex].value);");
	
	echo "</td>";
	echo "<td align=center>";
		

	echo "<a
href=\"../../includes/report_files/filters_customization.php?action=insert&
report_id=$report_id\" title=\"click to create new report format\"
onclick=\"\">Create Format</a> | ";
	$customization_details_array[0]["customization_privilege"]="d";
	if(!$custom_default_array)
		{
		if($customization_details_array[0]["customization_privilege"]=="e" || $customization_details_array[0]["customization_privilege"]=="d")
		{
		echo "<a
href=\"../../includes/report_files/filters_customization.php	?action=edit&
customization_id=$customization_id&report_id=$report_id\" title=\"click to edit
this report format\" onclick=\"\">Edit Format</a> | ";
		}
		if($customization_details_array[0]["customization_privilege"]=="d")
		{
		echo "<a onclick=\"if(confirm('Are You Sure? You Want to Delete this Report Format')){javascript:window.location='available_customization.php?customization_id=$customization_id&report_id=$report_id&action=delete';}\" style=\"cursor:pointer;\" title=\"click to delete this report format\" >Delete Format</a> | ";
		}
		}
	echo "<a onclick=\"javascript:window.open('copy_customization.php?customization_id=$customization_id&report_id=$report_id','','left=300,top=300,width=400,height=180,statusbar=no');\" style=\"cursor:pointer;\" title=\"click to copy this report format\">Clone</a> | ";
	
	echo "<a href=\"report_constraints.php?customization_id=$customization_id&customization_name=$customization_name&action=$report_status\"  title=\"click to change conditions\">Change Conditions</a>";
	echo " | "."<a href=\"javascript:Toggle('condition_table');\" title=\"click to view applied conditions\">Applied Conditions</a>";
	
	echo "</td><td valign=\"top\" align=center>";
	if($_GET["action"] != "print_report" )
		{
		showPrintableReportLink($query_string);
		}
	echo "</td></tr></table>";
	echo $form->generateInputTag("hidden",array("name"=>"report_id","value"=>$report_id));
	echo "</form>";

	$db->query="select
				smf.filter_name
				from
				$sys_master_filter_table smf
				join $sys_report_filter_table srf
				on smf.filter_id=srf.filter_id where srf.report_id=(select distinct(report_id) from $sys_report_customization_table where customization_id=$customization_id)";
	$db->runQuery();
	$fiter_name_arrays=$db->returnArrays();
	$filter_name_array_count=count($fiter_name_arrays);
	foreach($fiter_name_arrays  as $key=>$filter_name_array)
		{
		$filter_name=$filter_name_array["filter_name"];
		$post_filter_name=$_POST[$filter_name];

		if($post_filter_name[0]!="")
			{
			$post_filter_constraint_array=$_POST[$filter_name];

			$db->query="select
					form_display
					from
					$sys_master_filter_table
					where filter_name='$filter_name'";
			$db->runQuery();

			$display_filter_name_array=$db->returnArrays();
			$display_filter_name=$display_filter_name_array[0]["form_display"];
			$applied_condition_array[0][$display_filter_name]=implode(",",$post_filter_constraint_array);

			}

		}
		$c=count($group_name);
	if($group_name[0]!="")
	{
	foreach($group_name as $key=>$value)
		{

			$db->query="select
						display_name

					from
						$report_customization_table
					where column_id=(select column_id from $master_column_mapping_table 		where script_variable_name='$group_name[$key]' and report_id=$report_id) and 			customization_id=$customization_id";
			$db->runQuery();
			$display_group_name_array=$db->returnArrays();
			$applied_condition_array[0]["Groups Applied"].=$display_group_name_array[0]["display_name"];
			if($key<($c-1) && ($display_group_name_array[0]["display_name"]!=""))
			{

			$applied_condition_array[0]["Groups Applied"].=",";
			}
		}
	}
	if($date_type)
		{
		$db->query="select display_name from $master_report_customization_table where column_id=(select column_id from $master_column_mapping_table where db_column_name='".$date_type."' and report_id=$report_id)";
		$db->runQuery();
		$db_date_display_array=$db->returnArrays();
		$applied_condition_array[0]["Date Condition"]=$db_date_display_array[0]["display_name"]." ".$string_condition;
		}
	if($number_condition)
		{
		$db->query="select display_name from $master_report_customization_table where column_id=(select column_id from $master_column_mapping_table where db_column_name='".$number_condition."' and report_id=$report_id)";
		$db->runQuery();
		$db_number_display_array=$db->returnArrays();
		$applied_condition_array[0]["Number Condition"]=$db_number_display_array[0]["display_name"]." ".$string_condition1;
		}
	if($applied_condition_array[0]["Groups Applied"]=="")
		unset($applied_condition_array[0]["Groups Applied"]);

	echo "<div id=\"condition_table\" style=\"display:none;\">";
	if(!$applied_condition_array)
	{
	echo "<center><blink>No Conditions Applied</blink></center>";
	}
	else
	{
	echo "<h3>Applied Conditions</h3>";
	doVerticalTable($applied_condition_array,"75%","",true,true,"table_border1");
	}
	echo "</div>";


?>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">

function Toggle(item)
	{
	obj=document.getElementById(item);
	visible=(obj.style.display!="none")
	if (visible)
		{
		obj.style.display="none";
   		}
	 else
		{
      		obj.style.display="block";
		}
	}
function doSubmit(target)
	{
	if(target)
	document.custom_selection.submit();
	}
</script>